package com.gljx.web.controller;

import com.gljx.util.CommonExport;
import com.gljx.util.GetFid;
import com.gljx.util.GetFnumber;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.*;
import com.gljx.web.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 人力资源定级定薪
 * @author wjb
 *
 */
@Controller
@RequestMapping("/")
public class RemunerationascController {
	//自动注入
	@Autowired
    private DepartmentService departmentService;

	@Autowired
	private CustomLogService customLogService;

	@Autowired
	private RemunerationascService remunerationascService;

	@Autowired
	private RemunerationService remunerationService;

	@Autowired
	private PositiongradeService positiongradeService;
	/**
	 * 根据组织id查询定级定薪信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/queryRemunerationasc/look.action")
	public String selRemunerationasc(HttpServletRequest request,@RequestParam(value = "type",defaultValue = "") String type)throws Exception{
		//获取session中的登录信息
        User user = (User) request.getSession().getAttribute("user");
        List<Remunerationasc> remunerationascList=null;
        if(user!=null){
        	remunerationascList=remunerationascService.queryRemunerationascByCompanyId(user.getFcompanyid());
        }
        request.setAttribute("remunerationascList", remunerationascList);
        request.setAttribute("type",type);
		return "backHome/humanResource/remunerationasc/remunerationascList";
	}
	/**
	 * 跳转到增加定薪定级页面
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/intoRemunerationasc")
	public String intoRemunerationasc(HttpServletRequest request,String type)throws Exception{
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //获取部门
	    List<Department>departments = departmentService.queryAdminByController(user.getFcompanyid());
	    //获取组织
	    Department department = departmentService.queryAdminByFid(user.getFcompanyid());
	    //获取薪酬项目的信息
	    List<Remuneration> remunerationList=remunerationService.selectRemunerationList(user.getFcompanyid());
	    //获取职级职等信息
	    List<Positiongrade> positiongradeList=positiongradeService.selectPosttiongradeList(user.getFcompanyid());
	    request.setAttribute("remunerationList", remunerationList);
	    request.setAttribute("positiongradeList", positiongradeList);
	    request.setAttribute("department_remunerationasc", department);
	    request.setAttribute("departments_remunerationasc", departments);
	    request.setAttribute("type",type);
		return "backHome/humanResource/remunerationasc/addRemunerationasc";
	}
	/**
	 * 新增页面通过ajax传过来的id查找薪酬项目
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/selRemunerationById")
	@ResponseBody
	public Object selRemunerationById(@RequestParam(value="id",required=false)String id){
		List<Remuneration> remunerationList=new ArrayList<Remuneration>();
		String [] ids=id.split(",");
		for(int i=0;i<ids.length;i++){
			Remuneration remuneration=remunerationService.selectByPrimaryKey(ids[i]);
			remunerationList.add(remuneration);
		}
		return remunerationList;
	}
	/**
	 * 通过名称查找薪酬项目信息
	 * @param name
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/selRemunerationByName")
	@ResponseBody
	public Object selRemunerationByName(HttpServletRequest request,@RequestParam(value="name",required=false)String name)throws Exception{
		//获取session中的登录信息
        User user = (User) request.getSession().getAttribute("user");
		List<Remuneration> remunerationList=new ArrayList<Remuneration>();
		name.trim();//去空格
		if(name!=null &&!name.equals("")){
			remunerationList=remunerationService.selectRemunerationByFname(name, user.getFcompanyid());
		}else{
			remunerationList=remunerationService.selectRemunerationList(user.getFcompanyid());
		}
		//获取组织
	    Department department = departmentService.queryAdminByFid(user.getFcompanyid());
	    for (Remuneration remuneration : remunerationList) {
			remuneration.setCdepartment((String)department.getFnameL2());
		}
		return remunerationList;
	}
	/**
	 * 新增页面通过ajax传过来的id查找职级职等信息
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/selPositiongradeById")
	@ResponseBody
	public Object selPositiongradeById(@RequestParam(value="id",required=false)String id)throws Exception{
		List<Positiongrade> positiongradeList=new ArrayList<Positiongrade>();
		String [] ids=id.split(",");
		for(int i=0;i<ids.length;i++){
			Positiongrade positiongrade=positiongradeService.queryPositiongradeByFid(ids[i]);
			positiongradeList.add(positiongrade);
		}
		return positiongradeList;
	}
	/**
	 * 通过名称查找职级职等信息
	 * @param request
	 * @param name
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/selPositiongradeByName")
	@ResponseBody
	public Object selPositiongradeByName(HttpServletRequest request,@RequestParam(value="name",required=false)String name)throws Exception{
		//获取session中的登录信息
        User user = (User) request.getSession().getAttribute("user");
		List<Positiongrade> positiongradeList=new ArrayList<Positiongrade>();
		name.trim();//去空格
		if(name!=null &&!name.equals("")){
			positiongradeList=positiongradeService.selectPositiongradeByFname(name, user.getFcompanyid());
		}else{
			positiongradeList=positiongradeService.selectPosttiongradeList(user.getFcompanyid());
		}
		//获取组织
	    Department department = departmentService.queryAdminByFid(user.getFcompanyid());
	    for (Positiongrade positiongrade : positiongradeList) {
			positiongrade.setCdepartment((String)department.getFnameL2());
		}
		return positiongradeList;
	}
	/**
	 * 增加定薪定级信息
	 * @param request
	 * @param remunerationasc 需要增加信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/addRemunerationasc")
	public String addRemunerationasc(HttpServletRequest request,Remunerationasc remunerationasc)throws Exception{
		//获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        /*获取当前时间*/
        String createTime = TimeFormat.timeFormat(new Date());
        /*获取uuid*/
        remunerationasc.setFid(GetFid.UUIDIntoFid());
        //获取上一个编码
        /*String number = remunerationascService.selectFnumber(user.getFcompanyid());*/
        //获取编码
        String fnumber = GetFnumber.fnumbers("SFRE");
        remunerationasc.setFnumber(fnumber);
        remunerationasc.setCfficompanyid(user.getFcompanyid());
        remunerationasc.setFcreatorid(user.getFid());
        remunerationasc.setFcreatetime(createTime);
        remunerationasc.setFcontrolunitid(user.getFcompanyid());
        remunerationasc.setCreateName(user.getName());
        //增加
        int row=remunerationascService.addRemunerationasc(remunerationasc);
        String result="";
        if(row >0){
			result="1";
		}
        customLogService.addLog("人力资源", createTime + "对定薪定级进行操作", null, user.getFid());
		return "redirect:/DrivingSys/intoRemunerationasc?type="+result;
	}


	/**
	 * 根据fid删除
	 * @param request
	 * @param deleteId 被删除的fid
	 * @return
	 */
	@RequestMapping("/deleteRemunerationascByFid")
	public String deleteCarByFid(HttpServletRequest request,String[] deleteId,String fid){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//删除记录
		if("".equals(fid) || fid == null){
			for(int i = 0;i<deleteId.length;i++){
				remunerationascService.deleteByPrimaryKey(deleteId[i]);
			}
		}else{
			remunerationascService.deleteByPrimaryKey(fid);
		}
		//添加日志
		customLogService.addLog("人力资源", TimeFormat.timeFormat(new Date())+"对定薪定级删除操作", null, user.getFid());
		return "redirect:/DrivingSys/queryRemunerationasc/look.action";
	}

	/**
	 * 跳转到修改定薪定级页面 并且把需要修改的信息到过去
	 * @param request
	 * @param fid 需要修改的fid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/intoUpdateRemunerationasc")
	public String intoUpdateRemunerationasc(HttpServletRequest request,@RequestParam(value="fid",required=false)String fid)throws Exception{
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		Remunerationasc remunerationasc=remunerationascService.selectRemunerationascByFid(user.getFcompanyid(), fid);
		//获取组织
	    Department department = departmentService.queryAdminByFid(user.getFcompanyid());
	    //获取薪酬项目的信息
	    List<Remuneration> remunerationList=remunerationService.selectRemunerationList(user.getFcompanyid());
	    //获取职级职等信息
	    List<Positiongrade> positiongradeList=positiongradeService.selectPosttiongradeList(user.getFcompanyid());
	    request.setAttribute("department_remunerationasc", department);
	    request.setAttribute("remunerationasc", remunerationasc);
	    request.setAttribute("remunerationList", remunerationList);
	    request.setAttribute("positiongradeList", positiongradeList);
		return "backHome/humanResource/remunerationasc/updateRemunerationasc";
	}
	/**
	 * 修改定薪定级信息
	 * @param request
	 * @param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/updateRemunerationasc")
	public String updateRemunerationasc(HttpServletRequest request,Remunerationasc remunerationasc)throws Exception{
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//公司id
		remunerationasc.setCfficompanyid(user.getFcompanyid());
		//控制单元id
		remunerationasc.setFcontrolunitid(user.getFcompanyid());
		/*获取当前时间*/
        String updateTime = TimeFormat.timeFormat(new Date());
        //设置修改时间
        remunerationasc.setFlastupdatetime(updateTime);
        //设置修改人id
        remunerationasc.setFlastupdateuserid(user.getFid());
        //修改
        int row = remunerationascService.updateRemunerationascByFid(remunerationasc);
        String result ="";
        if(row > 0){
        	result="1";
		}
		return "redirect:/DrivingSys/queryRemunerationasc/look.action?type="+result;
	}
	/**
	 * 根据名称查找
	 * @param request
	 * @param fnameL2 需要查找的名称
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/selRemunerationascByName")
	public String selRemunerationascByName(HttpServletRequest request,String fnameL2)throws Exception{
		//获取session中的登录信息
        User user = (User) request.getSession().getAttribute("user");
        List<Remunerationasc> remunerationascList=null;
        remunerationascList=remunerationascService.selectRemunerationascByName(fnameL2, user.getFcompanyid());
        request.setAttribute("remunerationascList", remunerationascList);
        request.setAttribute("fnameL2",fnameL2);
		return "backHome/humanResource/remunerationasc/remunerationascList";
	}


	/**
	 * 人力资源 定薪定级 导入Excel
	 * @param response
	 * @param request
	 * @param fnameL2
	 */
	@RequestMapping(value = "/importRemunerationasc")
	public void importRemunerationasc(HttpServletResponse response,HttpServletRequest request,String fnameL2){
		//获取操作人信息
		User user = (User) request.getSession().getAttribute("user");
		//定义接收类型
		List<Object> data = new ArrayList<>();
		//查询数据
		List<Remunerationasc> remunerationascs =remunerationascService.selectRemunerationascByName(fnameL2,user.getFcompanyid());
		for (Remunerationasc remunerationasc: remunerationascs){
			data.add(remunerationasc.getFnumber());
			data.add(remunerationasc.getFnameL2());
			data.add(remunerationasc.getFsimplename());
			data.add(remunerationasc.getFdescriptionL2());
			data.add(remunerationasc.getFcreatetime());
			data.add(remunerationasc.getFlastupdatetime());
			data.add(remunerationasc.getCreateName());
			data.add(remunerationasc.getUpdateName());
			data.add(remunerationasc.getCfremunerationitem());
			data.add(remunerationasc.getZjzdName());
			data.add(remunerationasc.getCfamount());
		}
		//头部数据
		String head[] = {"编码", "名称", "简称", "描述", "创建时间", "最后修改时间", "创建者", "修改人","薪酬项目","职级职等","金额"};
		//长度
		Integer length[] = {9000, 5000, 5000, 5000, 5000, 5000, 3000, 3000,3000,3000,3000};
		try {
			//导出Excel
			CommonExport.importExcel(response, "定薪定级" + TimeFormat.getNowTime2() + ".xls", "定薪定级", data, head, length);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
